home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 4: GNU Archives / Linux Cubed Series 4 - GNU Archives.iso / gnu / binutils.7 / binutils / binutils-2.7 / ld / emultempl / aix.em next >
Encoding:
Text File  |  1996-07-04  |  27.1 KB  |  1,044 lines

  1. # This shell script emits a C file. -*- C -*-
  2. # It does some substitutions.
  3. cat >e${EMULATION_NAME}.c <<EOF
  4. /* This file is is generated by a shell script.  DO NOT EDIT! */
  5.  
  6. /* AIX emulation code for ${EMULATION_NAME}
  7.    Copyright (C) 1991, 1993, 1995 Free Software Foundation, Inc.
  8.    Written by Steve Chamberlain <sac@cygnus.com>
  9.    AIX support by Ian Lance Taylor <ian@cygnus.com>
  10.  
  11. This file is part of GLD, the Gnu Linker.
  12.  
  13. This program is free software; you can redistribute it and/or modify
  14. it under the terms of the GNU General Public License as published by
  15. the Free Software Foundation; either version 2 of the License, or
  16. (at your option) any later version.
  17.  
  18. This program is distributed in the hope that it will be useful,
  19. but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  21. GNU General Public License for more details.
  22.  
  23. You should have received a copy of the GNU General Public License
  24. along with this program; if not, write to the Free Software
  25. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
  26.  
  27. #define TARGET_IS_${EMULATION_NAME}
  28.  
  29. #include "bfd.h"
  30. #include "sysdep.h"
  31. #include "libiberty.h"
  32. #include "getopt.h"
  33. #include "bfdlink.h"
  34.  
  35. #include <ctype.h>
  36.  
  37. #include "ld.h"
  38. #include "ldmain.h"
  39. #include "ldemul.h"
  40. #include "ldfile.h"
  41. #include "ldmisc.h"
  42. #include "ldexp.h"
  43. #include "ldlang.h"
  44. #include "ldctor.h"
  45. #include "ldgram.h"
  46.  
  47. static void gld${EMULATION_NAME}_before_parse PARAMS ((void));
  48. static int gld${EMULATION_NAME}_parse_args PARAMS ((int, char **));
  49. static void gld${EMULATION_NAME}_after_open PARAMS ((void));
  50. static void gld${EMULATION_NAME}_before_allocation PARAMS ((void));
  51. static void gld${EMULATION_NAME}_read_file PARAMS ((const char *, boolean));
  52. static void gld${EMULATION_NAME}_free PARAMS ((PTR));
  53. static void gld${EMULATION_NAME}_find_relocs
  54.   PARAMS ((lang_statement_union_type *));
  55. static void gld${EMULATION_NAME}_find_exp_assignment PARAMS ((etree_type *));
  56. static char *gld${EMULATION_NAME}_get_script PARAMS ((int *isfile));
  57.  
  58. /* The file alignment required for each section.  */
  59. static unsigned long file_align;
  60.  
  61. /* The maximum size the stack is permitted to grow.  This is stored in
  62.    the a.out header.  */
  63. static unsigned long maxstack;
  64.  
  65. /* The maximum data size.  This is stored in the a.out header.  */
  66. static unsigned long maxdata;
  67.  
  68. /* Whether to perform garbage collection.  */
  69. static int gc = 1;
  70.  
  71. /* The module type to use.  */
  72. static unsigned short modtype = ('1' << 8) | 'L';
  73.  
  74. /* Whether the .text section must be read-only (i.e., no relocs
  75.    permitted).  */
  76. static int textro;
  77.  
  78. /* Whether to implement Unix like linker semantics.  */
  79. static int unix_ld;
  80.  
  81. /* Structure used to hold import file list.  */
  82.  
  83. struct filelist
  84. {
  85.   struct filelist *next;
  86.   const char *name;
  87. };
  88.  
  89. /* List of import files.  */
  90. static struct filelist *import_files;
  91.  
  92. /* List of export symbols read from the export files.  */
  93.  
  94. struct export_symbol_list
  95. {
  96.   struct export_symbol_list *next;
  97.   const char *name;
  98.   boolean syscall;
  99. };
  100.  
  101. static struct export_symbol_list *export_symbols;
  102.  
  103. /* This routine is called before anything else is done.  */
  104.  
  105. static void
  106. gld${EMULATION_NAME}_before_parse()
  107. {
  108. #ifndef TARGET_            /* I.e., if not generic.  */
  109.   ldfile_output_architecture = bfd_arch_${ARCH};
  110. #endif /* not TARGET_ */
  111. }
  112.  
  113. /* Handle AIX specific options.  */
  114.  
  115. static int
  116. gld${EMULATION_NAME}_parse_args (argc, argv)
  117.      int argc;
  118.      char **argv;
  119. {
  120.   int prevoptind = optind;
  121.   int prevopterr = opterr;
  122.   int indx;
  123.   int longind;
  124.   int optc;
  125.   long val;
  126.   char *end;
  127.  
  128. #define OPTION_IGNORE (300)
  129. #define OPTION_AUTOIMP (OPTION_IGNORE + 1)
  130. #define OPTION_ERNOTOK (OPTION_AUTOIMP + 1)
  131. #define OPTION_EROK (OPTION_ERNOTOK + 1)
  132. #define OPTION_EXPORT (OPTION_EROK + 1)
  133. #define OPTION_IMPORT (OPTION_EXPORT + 1)
  134. #define OPTION_LOADMAP (OPTION_IMPORT + 1)
  135. #define OPTION_MAXDATA (OPTION_LOADMAP + 1)
  136. #define OPTION_MAXSTACK (OPTION_MAXDATA + 1)
  137. #define OPTION_MODTYPE (OPTION_MAXSTACK + 1)
  138. #define OPTION_NOAUTOIMP (OPTION_MODTYPE + 1)
  139. #define OPTION_NOSTRCMPCT (OPTION_NOAUTOIMP + 1)
  140. #define OPTION_PD (OPTION_NOSTRCMPCT + 1)
  141. #define OPTION_PT (OPTION_PD + 1)
  142. #define OPTION_STRCMPCT (OPTION_PT + 1)
  143. #define OPTION_UNIX (OPTION_STRCMPCT + 1)
  144.  
  145.   static struct option longopts[] = {
  146.     {"basis", no_argument, NULL, OPTION_IGNORE},
  147.     {"bautoimp", no_argument, NULL, OPTION_AUTOIMP},
  148.     {"bcomprld", no_argument, NULL, OPTION_IGNORE},
  149.     {"bcrld", no_argument, NULL, OPTION_IGNORE},
  150.     {"bcror31", no_argument, NULL, OPTION_IGNORE},
  151.     {"bD", required_argument, NULL, OPTION_MAXDATA},
  152.     {"bE", required_argument, NULL, OPTION_EXPORT},
  153.     {"bernotok", no_argument, NULL, OPTION_ERNOTOK},
  154.     {"berok", no_argument, NULL, OPTION_EROK},
  155.     {"berrmsg", no_argument, NULL, OPTION_IGNORE},
  156.     {"bexport", required_argument, NULL, OPTION_EXPORT},
  157.     {"bf", no_argument, NULL, OPTION_ERNOTOK},
  158.     {"bgc", no_argument, &gc, 1},
  159.     {"bh", required_argument, NULL, OPTION_IGNORE},
  160.     {"bhalt", required_argument, NULL, OPTION_IGNORE},
  161.     {"bI", required_argument, NULL, OPTION_IMPORT},
  162.     {"bimport", required_argument, NULL, OPTION_IMPORT},
  163.     {"bl", required_argument, NULL, OPTION_LOADMAP},
  164.     {"bloadmap", required_argument, NULL, OPTION_LOADMAP},
  165.     {"bmaxdata", required_argument, NULL, OPTION_MAXDATA},
  166.     {"bmaxstack", required_argument, NULL, OPTION_MAXSTACK},
  167.     {"bM", required_argument, NULL, OPTION_MODTYPE},
  168.     {"bmodtype", required_argument, NULL, OPTION_MODTYPE},
  169.     {"bnoautoimp", no_argument, NULL, OPTION_NOAUTOIMP},
  170.     {"bnodelcsect", no_argument, NULL, OPTION_IGNORE},
  171.     {"bnoentry", no_argument, NULL, OPTION_IGNORE},
  172.     {"bnogc", no_argument, &gc, 0},
  173.     {"bnso", no_argument, NULL, OPTION_NOAUTOIMP},
  174.     {"bnostrcmpct", no_argument, NULL, OPTION_NOSTRCMPCT},
  175.     {"bnotextro", no_argument, &textro, 0},
  176.     {"bnro", no_argument, &textro, 0},
  177.     {"bpD", required_argument, NULL, OPTION_PD},
  178.     {"bpT", required_argument, NULL, OPTION_PT},
  179.     {"bro", no_argument, &textro, 1},
  180.     {"bS", required_argument, NULL, OPTION_MAXSTACK},
  181.     {"bso", no_argument, NULL, OPTION_AUTOIMP},
  182.     {"bstrcmpct", no_argument, NULL, OPTION_STRCMPCT},
  183.     {"btextro", no_argument, &textro, 1},
  184.     {"static", no_argument, NULL, OPTION_NOAUTOIMP},
  185.     {"unix", no_argument, NULL, OPTION_UNIX},
  186.     {NULL, no_argument, NULL, 0}
  187.   };
  188.  
  189.   /* Options supported by the AIX linker which we do not support: -f,
  190.      -S, -v, -Z, -bbindcmds, -bbinder, -bbindopts, -bcalls, -bcaps,
  191.      -bcror15, -bdebugopt, -bdbg, -bdelcsect, -bex?, -bfilelist, -bfl,
  192.      -bgcbypass, -bglink, -binsert, -bi, -bloadmap, -bl, -bmap, -bnl,
  193.      -bnobind, -bnocomprld, -bnocrld, -bnoerrmsg, -bnoglink,
  194.      -bnoloadmap, -bnl, -bnoobjreorder, -bnoquiet, -bnoreorder,
  195.      -bnotypchk, -bnox, -bquiet, -bR, -brename, -breorder, -btypchk,
  196.      -bx, -bX, -bxref.  */
  197.  
  198.   /* If the current option starts with -b, change the first : to an =.
  199.      The AIX linker uses : to separate the option from the argument;
  200.      changing it to = lets us treat it as a getopt option.  */
  201.   indx = optind;
  202.   if (indx == 0)
  203.     indx = 1;
  204.   if (indx < argc && strncmp (argv[indx], "-b", 2) == 0)
  205.     {
  206.       char *s;
  207.  
  208.       for (s = argv[indx]; *s != '\0'; s++)
  209.     {
  210.       if (*s == ':')
  211.         {
  212.           *s = '=';
  213.           break;
  214.         }
  215.     }
  216.     }
  217.  
  218.   opterr = 0;
  219.   optc = getopt_long_only (argc, argv, "-D:H:KT:z", longopts, &longind);
  220.   opterr = prevopterr;
  221.  
  222.   switch (optc)
  223.     {
  224.     default:
  225.       optind = prevoptind;
  226.       return 0;
  227.  
  228.     case 0:
  229.       /* Long option which just sets a flag.  */
  230.       break;
  231.  
  232.     case 'D':
  233.       val = strtol (optarg, &end, 0);
  234.       if (*end != '\0')
  235.     einfo ("%P: warning: ignoring invalid -D number %s\n", optarg);
  236.       else if (val != -1)
  237.     lang_section_start (".data", exp_intop (val));
  238.       break;
  239.  
  240.     case 'H':
  241.       val = strtoul (optarg, &end, 0);
  242.       if (*end != '\0'
  243.       || (val & (val - 1)) != 0)
  244.     einfo ("%P: warning: ignoring invalid -H number %s\n", optarg);
  245.       else
  246.     file_align = val;
  247.       break;
  248.  
  249.     case 'K':
  250.     case 'z':
  251.       /* FIXME: This should use the page size for the target system.  */
  252.       file_align = 4096;
  253.       break;
  254.  
  255.     case 'T':
  256.       /* On AIX this is the same as GNU ld -Ttext.  When we see -T
  257.          number, we assume the AIX option is intended.  Otherwise, we
  258.          assume the usual GNU ld -T option is intended.  We can't just
  259.          ignore the AIX option, because gcc passes it to the linker.  */
  260.       val = strtoul (optarg, &end, 0);
  261.       if (*end != '\0')
  262.     {
  263.       optind = prevoptind;
  264.       return 0;
  265.     }
  266.       lang_section_start (".text", exp_intop (val));
  267.       break;
  268.  
  269.     case OPTION_IGNORE:
  270.       break;
  271.  
  272.     case OPTION_AUTOIMP:
  273.       link_info.static_link = false;
  274.       break;
  275.  
  276.     case OPTION_ERNOTOK:
  277.       force_make_executable = false;
  278.       break;
  279.  
  280.     case OPTION_EROK:
  281.       force_make_executable = true;
  282.       break;
  283.  
  284.     case OPTION_EXPORT:
  285.       gld${EMULATION_NAME}_read_file (optarg, false);
  286.       break;
  287.  
  288.     case OPTION_IMPORT:
  289.       {
  290.     struct filelist *n;
  291.     struct filelist **flpp;
  292.  
  293.     n = (struct filelist *) xmalloc (sizeof (struct filelist));
  294.     n->next = NULL;
  295.     n->name = optarg;
  296.     flpp = &import_files;
  297.     while (*flpp != NULL)
  298.       flpp = &(*flpp)->next;
  299.     *flpp = n;
  300.       }
  301.       break;
  302.  
  303.     case OPTION_LOADMAP:
  304.       config.map_filename = optarg;
  305.       break;
  306.  
  307.     case OPTION_MAXDATA:
  308.       val = strtoul (optarg, &end, 0);
  309.       if (*end != '\0')
  310.     einfo ("%P: warning: ignoring invalid -bmaxdata number %s\n",
  311.            optarg);
  312.       else
  313.     maxdata = val;
  314.       break;
  315.  
  316.     case OPTION_MAXSTACK:
  317.       val = strtoul (optarg, &end, 0);
  318.       if (*end != '\0')
  319.     einfo ("%P: warning: ignoring invalid -bmaxstack number %s\n",
  320.            optarg);
  321.       else
  322.     maxstack = val;
  323.       break;
  324.  
  325.     case OPTION_MODTYPE:
  326.       if (*optarg == 'S')
  327.     {
  328.       link_info.shared = true;
  329.       ++optarg;
  330.     }
  331.       if (*optarg == '\0' || optarg[1] == '\0')
  332.     einfo ("%P: warning: ignoring invalid module type %s\n", optarg);
  333.       else
  334.     modtype = (*optarg << 8) | optarg[1];
  335.       break;
  336.  
  337.     case OPTION_NOAUTOIMP:
  338.       link_info.static_link = true;
  339.       break;
  340.  
  341.     case OPTION_NOSTRCMPCT:
  342.       link_info.traditional_format = true;
  343.       break;
  344.  
  345.     case OPTION_PD:
  346.       /* This sets the page that the .data section is supposed to
  347.          start on.  The offset within the page should still be the
  348.          offset within the file, so we need to build an appropriate
  349.          expression.  */
  350.       val = strtoul (optarg, &end, 0);
  351.       if (*end != '\0')
  352.     einfo ("%P: warning: ignoring invalid -pD number %s\n", optarg);
  353.       else
  354.     {
  355.       etree_type *t;
  356.  
  357.       t = exp_binop ('+',
  358.              exp_intop (val),
  359.              exp_binop ('&',
  360.                     exp_nameop (NAME, "."),
  361.                     exp_intop (0xfff)));
  362.       t = exp_binop ('&',
  363.              exp_binop ('+', t, exp_intop (7)),
  364.              exp_intop (~ (bfd_vma) 7));
  365.       lang_section_start (".data", t);
  366.     }
  367.       break;
  368.  
  369.     case OPTION_PT:
  370.       /* This set the page that the .text section is supposed to start
  371.          on.  The offset within the page should still be the offset
  372.          within the file.  */
  373.       val = strtoul (optarg, &end, 0);
  374.       if (*end != '\0')
  375.     einfo ("%P: warning: ignoring invalid -pT number %s\n", optarg);
  376.       else
  377.     {
  378.       etree_type *t;
  379.  
  380.       t = exp_binop ('+',
  381.              exp_intop (val),
  382.              exp_nameop (SIZEOF_HEADERS, NULL));
  383.       t = exp_binop ('&',
  384.              exp_binop ('+', t, exp_intop (7)),
  385.              exp_intop (~ (bfd_vma) 7));
  386.       lang_section_start (".text", t);
  387.     }
  388.       break;
  389.  
  390.     case OPTION_STRCMPCT:
  391.       link_info.traditional_format = false;
  392.       break;
  393.  
  394.     case OPTION_UNIX:
  395.       unix_ld = true;
  396.       break;
  397.     }
  398.  
  399.   return 1;
  400. }
  401.  
  402. /* This is called when an input file can not be recognized as a BFD
  403.    object or an archive.  If the file starts with #!, we must treat it
  404.    as an import file.  This is for AIX compatibility.  */
  405.  
  406. static boolean
  407. gld${EMULATION_NAME}_unrecognized_file (entry)
  408.      lang_input_statement_type *entry;
  409. {
  410.   FILE *e;
  411.   boolean ret;
  412.  
  413.   e = fopen (entry->filename, FOPEN_RT);
  414.   if (e == NULL)
  415.     return false;
  416.  
  417.   ret = false;
  418.  
  419.   if (getc (e) == '#' && getc (e) == '!')
  420.     {
  421.       struct filelist *n;
  422.       struct filelist **flpp;
  423.  
  424.       n = (struct filelist *) xmalloc (sizeof (struct filelist));
  425.       n->next = NULL;
  426.       n->name = entry->filename;
  427.       flpp = &import_files;
  428.       while (*flpp != NULL)
  429.     flpp = &(*flpp)->next;
  430.       *flpp = n;
  431.  
  432.       ret = true;
  433.       entry->loaded = true;
  434.     }
  435.  
  436.   fclose (e);
  437.  
  438.   return ret;
  439. }
  440.  
  441. /* This is called after the input files have been opened.  */
  442.  
  443. static void
  444. gld${EMULATION_NAME}_after_open ()
  445. {
  446.   boolean r;
  447.   struct set_info *p;
  448.  
  449.   /* Call ldctor_build_sets, after pretending that this is a
  450.      relocateable link.  We do this because AIX requires relocation
  451.      entries for all references to symbols, even in a final
  452.      executable.  Of course, we only want to do this if we are
  453.      producing an XCOFF output file.  */
  454.   r = link_info.relocateable;
  455.   if (strstr (bfd_get_target (output_bfd), "xcoff") != NULL)
  456.     link_info.relocateable = true;
  457.   ldctor_build_sets ();
  458.   link_info.relocateable = r;
  459.  
  460.   /* For each set, record the size, so that the XCOFF backend can
  461.      output the correct csect length.  */
  462.   for (p = sets; p != (struct set_info *) NULL; p = p->next)
  463.     {
  464.       bfd_size_type size;
  465.  
  466.       /* If the symbol is defined, we may have been invoked from
  467.      collect, and the sets may already have been built, so we do
  468.      not do anything.  */
  469.       if (p->h->type == bfd_link_hash_defined
  470.       || p->h->type == bfd_link_hash_defweak)
  471.     continue;
  472.  
  473.       if (p->reloc != BFD_RELOC_CTOR)
  474.     {
  475.       /* Handle this if we need to.  */
  476.       abort ();
  477.     }
  478.  
  479.       size = (p->count + 2) * 4;
  480.       if (! bfd_xcoff_link_record_set (output_bfd, &link_info, p->h, size))
  481.     einfo ("%F%P: bfd_xcoff_link_record_set failed: %E\n");
  482.     }
  483. }
  484.  
  485. /* This is called after the sections have been attached to output
  486.    sections, but before any sizes or addresses have been set.  */
  487.  
  488. static void
  489. gld${EMULATION_NAME}_before_allocation ()
  490. {
  491.   struct filelist *fl;
  492.   struct export_symbol_list *el;
  493.   char *libpath;
  494.   asection *special_sections[6];
  495.   int i;
  496.  
  497.   /* Handle the import and export files, if any.  */
  498.   for (fl = import_files; fl != NULL; fl = fl->next)
  499.     gld${EMULATION_NAME}_read_file (fl->name, true);
  500.   for (el = export_symbols; el != NULL; el = el->next)
  501.     {
  502.       struct bfd_link_hash_entry *h;
  503.  
  504.       h = bfd_link_hash_lookup (link_info.hash, el->name, false, false, false);
  505.       if (h == NULL)
  506.     einfo ("%P%F: bfd_link_hash_lookup of export symbol failed: %E\n");
  507.       if (! bfd_xcoff_export_symbol (output_bfd, &link_info, h, el->syscall))
  508.     einfo ("%P%F: bfd_xcoff_export_symbol failed: %E\n");
  509.     }
  510.  
  511.   /* Track down all relocations called for by the linker script (these
  512.      are typically constructor/destructor entries created by
  513.      CONSTRUCTORS) and let the backend know it will need to create
  514.      .loader relocs for them.  */
  515.   lang_for_each_statement (gld${EMULATION_NAME}_find_relocs);
  516.  
  517.   /* We need to build LIBPATH from the -L arguments.  If any -rpath
  518.      arguments were used, though, we use -rpath instead, as a GNU
  519.      extension.  */
  520.   if (command_line.rpath != NULL)
  521.     libpath = command_line.rpath;
  522.   else if (search_head == NULL)
  523.     libpath = (char *) "";
  524.   else
  525.     {
  526.       size_t len;
  527.       search_dirs_type *search;
  528.  
  529.       len = strlen (search_head->name);
  530.       libpath = xmalloc (len + 1);
  531.       strcpy (libpath, search_head->name);
  532.       for (search = search_head->next; search != NULL; search = search->next)
  533.     {
  534.       size_t nlen;
  535.  
  536.       nlen = strlen (search->name);
  537.       libpath = xrealloc (libpath, len + nlen + 2);
  538.       libpath[len] = ':';
  539.       strcpy (libpath + len + 1, search->name);
  540.       len += nlen + 1;
  541.     }
  542.     }
  543.  
  544.   /* Let the XCOFF backend set up the .loader section.  */
  545.   if (! bfd_xcoff_size_dynamic_sections (output_bfd, &link_info, libpath,
  546.                      entry_symbol, file_align,
  547.                      maxstack, maxdata,
  548.                      gc && ! unix_ld ? true : false,
  549.                      modtype,
  550.                      textro ? true : false,
  551.                      unix_ld,
  552.                      special_sections))
  553.     einfo ("%P%F: failed to set dynamic section sizes: %E\n");
  554.  
  555.   /* Look through the special sections, and put them in the right
  556.      place in the link ordering.  This is especially magic.  */
  557.   for (i = 0; i < 6; i++)
  558.     {
  559.       asection *sec;
  560.       lang_output_section_statement_type *os;
  561.       lang_statement_union_type **pls;
  562.       lang_input_section_type *is;
  563.       const char *oname;
  564.       boolean start;
  565.  
  566.       sec = special_sections[i];
  567.       if (sec == NULL)
  568.     continue;
  569.  
  570.       /* Remove this section from the list of the output section.
  571.          This assumes we know what the script looks like.  */
  572.       is = NULL;
  573.       os = lang_output_section_find (sec->output_section->name);
  574.       if (os == NULL)
  575.     einfo ("%P%F: can't find output section %s\n",
  576.            sec->output_section->name);
  577.       for (pls = &os->children.head; *pls != NULL; pls = &(*pls)->next)
  578.     {
  579.       if ((*pls)->header.type == lang_input_section_enum
  580.           && (*pls)->input_section.section == sec)
  581.         {
  582.           is = (lang_input_section_type *) *pls;
  583.           *pls = (*pls)->next;
  584.           break;
  585.         }
  586.       if ((*pls)->header.type == lang_wild_statement_enum)
  587.         {
  588.           lang_statement_union_type **pwls;
  589.  
  590.           for (pwls = &(*pls)->wild_statement.children.head;
  591.            *pwls != NULL;
  592.            pwls = &(*pwls)->next)
  593.         {
  594.           if ((*pwls)->header.type == lang_input_section_enum
  595.               && (*pwls)->input_section.section == sec)
  596.             {
  597.               is = (lang_input_section_type *) *pwls;
  598.               *pwls = (*pwls)->next;
  599.               break;
  600.             }
  601.         }
  602.           if (is != NULL)
  603.         break;
  604.         }
  605.     }    
  606.  
  607.       if (is == NULL)
  608.     einfo ("%P%F: can't find %s in output section\n",
  609.            bfd_get_section_name (sec->owner, sec));
  610.  
  611.       /* Now figure out where the section should go.  */
  612.       switch (i)
  613.     {
  614.     default: /* to avoid warnings */
  615.     case 0:
  616.       /* _text */
  617.       oname = ".text";
  618.       start = true;
  619.       break;
  620.     case 1:
  621.       /* _etext */
  622.       oname = ".text";
  623.       start = false;
  624.       break;
  625.     case 2:
  626.       /* _data */
  627.       oname = ".data";
  628.       start = true;
  629.       break;
  630.     case 3:
  631.       /* _edata */
  632.       oname = ".data";
  633.       start = false;
  634.       break;
  635.     case 4:
  636.     case 5:
  637.       /* _end and end */
  638.       oname = ".bss";
  639.       start = false;
  640.       break;
  641.     }
  642.  
  643.       os = lang_output_section_find (oname);
  644.  
  645.       if (start)
  646.     {
  647.       is->header.next = os->children.head;
  648.       os->children.head = (lang_statement_union_type *) is;
  649.     }
  650.       else
  651.     {
  652.       is->header.next = NULL;
  653.       lang_statement_append (&os->children,
  654.                  (lang_statement_union_type *) is,
  655.                  &is->header.next);
  656.     }
  657.     }
  658. }
  659.  
  660. /* Read an import or export file.  For an import file, this is called
  661.    by the before_allocation emulation routine.  For an export file,
  662.    this is called by the parse_args emulation routine.  */
  663.  
  664. static void
  665. gld${EMULATION_NAME}_read_file (filename, import)
  666.      const char *filename;
  667.      boolean import;
  668. {
  669.   struct obstack *o;
  670.   FILE *f;
  671.   int lineno;
  672.   int c;
  673.   boolean keep;
  674.   const char *imppath;
  675.   const char *impfile;
  676.   const char *impmember;
  677.  
  678.   o = (struct obstack *) xmalloc (sizeof (struct obstack));
  679.   obstack_specify_allocation (o, 0, 0, xmalloc, gld${EMULATION_NAME}_free);
  680.  
  681.   f = fopen (filename, FOPEN_RT);
  682.   if (f == NULL)
  683.     {
  684.       bfd_set_error (bfd_error_system_call);
  685.       einfo ("%F%s: %E\n", filename);
  686.     }
  687.  
  688.   keep = false;
  689.  
  690.   imppath = NULL;
  691.   impfile = NULL;
  692.   impmember = NULL;
  693.  
  694.   lineno = 0;
  695.   while ((c = getc (f)) != EOF)
  696.     {
  697.       char *s;
  698.       char *symname;
  699.       boolean syscall;
  700.       bfd_vma address;
  701.       struct bfd_link_hash_entry *h;
  702.  
  703.       if (c != '\n')
  704.     {
  705.       obstack_1grow (o, c);
  706.       continue;
  707.     }
  708.  
  709.       obstack_1grow (o, '\0');
  710.       ++lineno;
  711.  
  712.       s = (char *) obstack_base (o);
  713.       while (isspace ((unsigned char) *s))
  714.     ++s;
  715.       if (*s == '\0'
  716.       || *s == '*'
  717.       || (*s == '#' && s[1] == ' ')
  718.       || (! import && *s == '#' && s[1] == '!'))
  719.     {
  720.       obstack_free (o, obstack_base (o));
  721.       continue;
  722.     }
  723.  
  724.       if (*s == '#' && s[1] == '!')
  725.     {
  726.       s += 2;
  727.       while (isspace ((unsigned char) *s))
  728.         ++s;
  729.       if (*s == '\0')
  730.         {
  731.           imppath = NULL;
  732.           impfile = NULL;
  733.           impmember = NULL;
  734.           obstack_free (o, obstack_base (o));
  735.         }
  736.       else if (*s == '(')
  737.         einfo ("%F%s%d: #! ([member]) is not supported in import files",
  738.            filename, lineno);
  739.       else
  740.         {
  741.           char cs;
  742.           char *file;
  743.  
  744.           (void) obstack_finish (o);
  745.           keep = true;
  746.           imppath = s;
  747.           file = NULL;
  748.           while (! isspace ((unsigned char) *s) && *s != '(' && *s != '\0')
  749.         {
  750.           if (*s == '/')
  751.             file = s + 1;
  752.           ++s;
  753.         }
  754.           if (file != NULL)
  755.         {
  756.           file[-1] = '\0';
  757.           impfile = file;
  758.           if (imppath == file - 1)
  759.             imppath = "/";
  760.         }
  761.           else
  762.         {
  763.           impfile = imppath;
  764.           imppath = "";
  765.         }
  766.           cs = *s;
  767.           *s = '\0';
  768.           while (isspace ((unsigned char) cs))
  769.         {
  770.           ++s;
  771.           cs = *s;
  772.         }
  773.           if (cs != '(')
  774.         {
  775.           impmember = "";
  776.           if (cs != '\0')
  777.             einfo ("%s:%d: warning: syntax error in import file\n",
  778.                filename, lineno);
  779.         }
  780.           else
  781.         {
  782.           ++s;
  783.           impmember = s;
  784.           while (*s != ')' && *s != '\0')
  785.             ++s;
  786.           if (*s == ')')
  787.             *s = '\0';
  788.           else
  789.             einfo ("%s:%d: warning: syntax error in import file\n",
  790.                filename, lineno);
  791.         }
  792.         }
  793.  
  794.       continue;
  795.     }
  796.  
  797.       /* This is a symbol to be imported or exported.  */
  798.       symname = s;
  799.       syscall = false;
  800.       address = (bfd_vma) -1;
  801.  
  802.       while (! isspace ((unsigned char) *s) && *s != '\0')
  803.     ++s;
  804.       if (*s != '\0')
  805.     {
  806.       char *se;
  807.  
  808.       *s++ = '\0';
  809.  
  810.       while (isspace ((unsigned char) *s))
  811.         ++s;
  812.  
  813.       se = s;
  814.       while (! isspace ((unsigned char) *se) && *se != '\0')
  815.         ++se;
  816.       if (*se != '\0')
  817.         {
  818.           *se++ = '\0';
  819.           while (isspace ((unsigned char) *se))
  820.         ++se;
  821.           if (*se != '\0')
  822.         einfo ("%s%d: warning: syntax error in import/export file\n",
  823.                filename, lineno);
  824.         }
  825.  
  826.       if (strcasecmp (s, "svc") == 0
  827.           || strcasecmp (s, "syscall") == 0)
  828.         syscall = true;
  829.       else
  830.         {
  831.           char *end;
  832.  
  833.           address = strtoul (s, &end, 0);
  834.           if (*end != '\0')
  835.         einfo ("%s:%d: warning: syntax error in import/export file\n",
  836.                filename, lineno);
  837.         }
  838.     }
  839.  
  840.       if (! import)
  841.     {
  842.       struct export_symbol_list *n;
  843.  
  844.       ldlang_add_undef (symname);
  845.       n = ((struct export_symbol_list *)
  846.            xmalloc (sizeof (struct export_symbol_list)));
  847.       n->next = export_symbols;
  848.       n->name = buystring (symname);
  849.       n->syscall = syscall;
  850.       export_symbols = n;
  851.     }
  852.       else
  853.     {
  854.       h = bfd_link_hash_lookup (link_info.hash, symname, false, false,
  855.                     true);
  856.       if (h == NULL || h->type == bfd_link_hash_new)
  857.         {
  858.           /* We can just ignore attempts to import an unreferenced
  859.          symbol.  */
  860.         }
  861.       else
  862.         {
  863.           if (! bfd_xcoff_import_symbol (output_bfd, &link_info, h,
  864.                          address, imppath, impfile,
  865.                          impmember))
  866.         einfo ("%X%s:%d: failed to import symbol %s: %E\n",
  867.                filename, lineno, symname);
  868.         }
  869.     }
  870.  
  871.       obstack_free (o, obstack_base (o));
  872.     }
  873.  
  874.   if (obstack_object_size (o) > 0)
  875.     {
  876.       einfo ("%s:%d: warning: ignoring unterminated last line\n",
  877.          filename, lineno);
  878.       obstack_free (o, obstack_base (o));
  879.     }
  880.  
  881.   if (! keep)
  882.     {
  883.       obstack_free (o, NULL);
  884.       free (o);
  885.     }
  886. }
  887.  
  888. /* This routine saves us from worrying about declaring free.  */
  889.  
  890. static void
  891. gld${EMULATION_NAME}_free (p)
  892.      PTR p;
  893. {
  894.   free (p);
  895. }
  896.  
  897. /* This is called by the before_allocation routine via
  898.    lang_for_each_statement.  It looks for relocations and assignments
  899.    to symbols.  */
  900.  
  901. static void
  902. gld${EMULATION_NAME}_find_relocs (s)
  903.      lang_statement_union_type *s;
  904. {
  905.   if (s->header.type == lang_reloc_statement_enum)
  906.     {
  907.       lang_reloc_statement_type *rs;
  908.  
  909.       rs = &s->reloc_statement;
  910.       if (rs->name == NULL)
  911.     einfo ("%F%P: only relocations against symbols are permitted\n");
  912.       if (! bfd_xcoff_link_count_reloc (output_bfd, &link_info, rs->name))
  913.     einfo ("%F%P: bfd_xcoff_link_count_reloc failed: %E\n");
  914.     }
  915.  
  916.   if (s->header.type == lang_assignment_statement_enum)
  917.     gld${EMULATION_NAME}_find_exp_assignment (s->assignment_statement.exp);
  918. }
  919.  
  920. /* Look through an expression for an assignment statement.  */
  921.  
  922. static void
  923. gld${EMULATION_NAME}_find_exp_assignment (exp)
  924.      etree_type *exp;
  925. {
  926.   struct bfd_link_hash_entry *h;
  927.  
  928.   switch (exp->type.node_class)
  929.     {
  930.     case etree_provide:
  931.       h = bfd_link_hash_lookup (link_info.hash, exp->assign.dst,
  932.                 false, false, false);
  933.       if (h == NULL)
  934.     break;
  935.       /* Fall through.  */
  936.     case etree_assign:
  937.       if (strcmp (exp->assign.dst, ".") != 0)
  938.     {
  939.       if (! bfd_xcoff_record_link_assignment (output_bfd, &link_info,
  940.                           exp->assign.dst))
  941.         einfo ("%P%F: failed to record assignment to %s: %E\n",
  942.            exp->assign.dst);
  943.     }
  944.       gld${EMULATION_NAME}_find_exp_assignment (exp->assign.src);
  945.       break;
  946.  
  947.     case etree_binary:
  948.       gld${EMULATION_NAME}_find_exp_assignment (exp->binary.lhs);
  949.       gld${EMULATION_NAME}_find_exp_assignment (exp->binary.rhs);
  950.       break;
  951.  
  952.     case etree_trinary:
  953.       gld${EMULATION_NAME}_find_exp_assignment (exp->trinary.cond);
  954.       gld${EMULATION_NAME}_find_exp_assignment (exp->trinary.lhs);
  955.       gld${EMULATION_NAME}_find_exp_assignment (exp->trinary.rhs);
  956.       break;
  957.  
  958.     case etree_unary:
  959.       gld${EMULATION_NAME}_find_exp_assignment (exp->unary.child);
  960.       break;
  961.  
  962.     default:
  963.       break;
  964.     }
  965. }
  966.  
  967. static char *
  968. gld${EMULATION_NAME}_get_script(isfile)
  969.      int *isfile;
  970. EOF
  971.  
  972. if test -n "$COMPILE_IN"
  973. then
  974. # Scripts compiled in.
  975.  
  976. # sed commands to quote an ld script as a C string.
  977. sc="-f ${srcdir}/emultempl/stringify.sed"
  978.  
  979. cat >>e${EMULATION_NAME}.c <<EOF
  980. {                 
  981.   *isfile = 0;
  982.  
  983.   if (link_info.relocateable == true && config.build_constructors == true)
  984.     return
  985. EOF
  986. sed $sc ldscripts/${EMULATION_NAME}.xu                     >> e${EMULATION_NAME}.c
  987. echo '  ; else if (link_info.relocateable == true) return' >> e${EMULATION_NAME}.c
  988. sed $sc ldscripts/${EMULATION_NAME}.xr                     >> e${EMULATION_NAME}.c
  989. echo '  ; else if (!config.text_read_only) return'         >> e${EMULATION_NAME}.c
  990. sed $sc ldscripts/${EMULATION_NAME}.xbn                    >> e${EMULATION_NAME}.c
  991. echo '  ; else if (!config.magic_demand_paged) return'     >> e${EMULATION_NAME}.c
  992. sed $sc ldscripts/${EMULATION_NAME}.xn                     >> e${EMULATION_NAME}.c
  993. echo '  ; else return'                                     >> e${EMULATION_NAME}.c
  994. sed $sc ldscripts/${EMULATION_NAME}.x                      >> e${EMULATION_NAME}.c
  995. echo '; }'                                                 >> e${EMULATION_NAME}.c
  996.  
  997. else
  998. # Scripts read from the filesystem.
  999.  
  1000. cat >>e${EMULATION_NAME}.c <<EOF
  1001. {                 
  1002.   *isfile = 1;
  1003.  
  1004.   if (link_info.relocateable == true && config.build_constructors == true)
  1005.     return "ldscripts/${EMULATION_NAME}.xu";
  1006.   else if (link_info.relocateable == true)
  1007.     return "ldscripts/${EMULATION_NAME}.xr";
  1008.   else if (!config.text_read_only)
  1009.     return "ldscripts/${EMULATION_NAME}.xbn";
  1010.   else if (!config.magic_demand_paged)
  1011.     return "ldscripts/${EMULATION_NAME}.xn";
  1012.   else
  1013.     return "ldscripts/${EMULATION_NAME}.x";
  1014. }
  1015. EOF
  1016.  
  1017. fi
  1018.  
  1019. cat >>e${EMULATION_NAME}.c <<EOF
  1020.  
  1021. struct ld_emulation_xfer_struct ld_${EMULATION_NAME}_emulation = 
  1022. {
  1023.   gld${EMULATION_NAME}_before_parse,
  1024.   syslib_default,
  1025.   hll_default,
  1026.   after_parse_default,
  1027.   gld${EMULATION_NAME}_after_open,
  1028.   after_allocation_default,
  1029.   set_output_arch_default,
  1030.   ldemul_default_target,
  1031.   gld${EMULATION_NAME}_before_allocation,
  1032.   gld${EMULATION_NAME}_get_script,
  1033.   "${EMULATION_NAME}",
  1034.   "${OUTPUT_FORMAT}",
  1035.   0,    /* finish */
  1036.   0,    /* create_output_section_statements */
  1037.   0,    /* open_dynamic_archive */
  1038.   0,    /* place_orphan */
  1039.   0,    /* set_symbols */
  1040.   gld${EMULATION_NAME}_parse_args,
  1041.   gld${EMULATION_NAME}_unrecognized_file
  1042. };
  1043. EOF
  1044.